package com.example.feidaolumanager.pages.ui.my

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.paint
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.fragment.app.Fragment
import com.example.feidaolumanager.Constants
import com.example.feidaolumanager.LoginActivity
import com.example.feidaolumanager.R
import com.example.feidaolumanager.databinding.FragmentMineBinding
import com.example.feidaolumanager.pages.SettingActivity
import com.example.feidaolumanager.pojo.User

/**
 * A simple [Fragment] subclass.
 * Use the [MineFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class MineFragment : Fragment() {

    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        val binding = FragmentMineBinding.inflate(inflater, container, false)
        val sharedPref: SharedPreferences = requireActivity().getSharedPreferences(Constants.store_key, Context.MODE_PRIVATE)

        val view = binding.root

        val user: User = activity?.intent?.getSerializableExtra("user") as User

        binding.composeView.setContent {

            Scaffold(topBar = {
                TopAppBar(title = {
                    Text("我的", color = Color.White)
                },
                    colors = TopAppBarDefaults.smallTopAppBarColors(
                        containerColor = Color(0, 172, 98)
                    ),
                    actions = {
                        Icon(imageVector = Icons.Default.Settings,
                            tint = Color.White,
                            contentDescription = "跳转到分享App页面",
                            modifier = Modifier.clickable {
                                startActivity(
                                    Intent(
                                        requireActivity(),
                                        SettingActivity::class.java
                                    )
                                )
                            })
                    })
            }) {
                Column(
                    modifier = Modifier
                        .padding(it)
                        .fillMaxHeight()
                        .paint(
                            painter = painterResource(id = R.drawable.bg),
                            contentScale = ContentScale.FillBounds
                        ),
                    horizontalAlignment = Alignment.CenterHorizontally,
                ) {
                    Column(
                        modifier = Modifier
                            .height(200.dp)
                            .fillMaxWidth(),
                        verticalArrangement = Arrangement.SpaceAround,
                        horizontalAlignment = Alignment.CenterHorizontally,
                    ) {

                        TextField(
                            value = user.name,
                            onValueChange = {

                            },
                            label = {
                                Text(text = "用户名")
                            },
                            modifier = Modifier.background(color = Color.Transparent),
                            colors = TextFieldDefaults.colors(
                                disabledTextColor = Color.White,
                                focusedContainerColor = Color.Transparent,
                                unfocusedContainerColor = Color.Transparent,
                                disabledContainerColor = Color.Transparent,
                                disabledLabelColor = Color.Black,
                            ),
                            enabled = false,
                            textStyle = TextStyle(fontSize = 18.sp)
                        )

                        val roles = listOf("施工方", "街道办", "住建局", "环保局")

                        TextField(value = roles[user.level - 1],
                            onValueChange = {},
                            enabled = false,
                            colors = TextFieldDefaults.colors(
                                disabledContainerColor = Color.Transparent,
                                disabledTextColor = Color.White,
                                disabledLabelColor = Color.Black
                            ),
                            label = {
                                Text(text = "角色")
                            })
                        TextField(
                            value = user.account,
                            onValueChange = {

                            },
                            label = {
                                Text(text = "账号")
                            },
                            modifier = Modifier.background(color = Color.Transparent),
                            colors = TextFieldDefaults.textFieldColors(
                                containerColor = Color.Transparent,
                                disabledTextColor = Color.White,
                                disabledLabelColor = Color.Black
                            ),
                            enabled = false,
                            textStyle = TextStyle(fontSize = 18.sp)
                        )
                    }

                    Spacer(modifier = Modifier.height(10.dp))

                    Button(onClick = {
                        // 删除本地存储的用户信息
                        Log.e(this@MineFragment.toString(), "onCreateView: $sharedPref")
                        with(sharedPref.edit()) {
                            remove("user")
                            clear()
                            apply()
                        }

                        // 跳转登录界面
                        requireActivity().startActivity(Intent(activity, LoginActivity::class.java))

                        // 销毁当前界面
                        requireActivity().finish()

                    }) {
                        Text(text = "退出登录")
                    }
                }
            }

        }
        return view
    }
}